home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
ast_comp
/
cpp-kit.lha
/
c++kit
/
Parse.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-11
|
4KB
|
223 lines
#include "Parse.H"
Parse::Parse(char * file)
: _state(And), _mark(_marks), _retn(0)
{
_scan = new Scan(file);
*_mark = _scan->mark();
_mark++;
}
void
Parse::open(char * file)
{
_state = And;
_mark = _marks;
_retn = 0;
_scan = new Scan(file);
*_mark = _scan->mark();
_mark++;
}
Parse::Parse(Scan& scan)
: _state(And), _mark(_marks), _retn(0)
{
_scan = &scan;
*_mark = _scan->mark();
_mark++;
}
void
Parse::open(Scan& scan)
{
_state = And;
_mark = _marks;
_retn = 0;
_scan = &scan;
*_mark = _scan->mark();
_mark++;
}
Parse&
Parse::operator , (char c)
{
Token t;
if( _state == And ) {
if( _scan->match(c, t) == 0 ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (char * s)
{
Token t;
if( _state == And ) {
if( _scan->match(s, t) == 0 ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (Parse& (*func)(Parse&) )
{
return func(*this);
}
Parse&
OR(Parse& P)
{
if( P._state == Parse::Fail ) {
P._state = Parse::And;
P._mark--;
P._scan->back(*P._mark);
}
else if( P._state == Parse::And ) {
P._state = Parse::Succ;
}
return P;
}
Parse&
Parse::operator , (const NUMBER& token)
{
if( _state == And ) {
if( !_scan->number(*(token._token)) ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (const STRING& token)
{
if( _state == And ) {
if( !_scan->string(*(token._token)) ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (const IDENT& token)
{
if( _state == And ) {
if( !_scan->identifier(*(token._token)) ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (const TOKEN& token)
{
if( _state == And ) {
if( !_scan->token(*(token._token)) ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (const CHARAC& token)
{
if( _state == And ) {
if( !_scan->character(*(token._token)) ) {
_state = Fail;
}
}
return *this;
}
Parse&
Parse::operator , (const CHOOSE& choice)
{
if( _state == And ) {
_retn = choice._retn;
}
return *this;
}
Parse&
EOFL(Parse& P)
{
if( P._state == Parse::And ) {
if( !P._scan->eof() ) {
P._state = Parse::Fail;
}
}
return P;
}
Parse&
Parse::operator , (const MATCH0& M0)
{
if( _state == And) {
int (*func)(Parse&) = M0._func;
*_mark = _scan->mark();
_mark++;
if( (func)(*this) == 0 ) {
_state = Fail;
}
_mark--;
}
return *this;
}
template <class T> Parse&
operator , (Parse& P, const MATCH1<T>& M1)
{
if( P._state == Parse::And) {
int (*func)(Parse&, T&) = M1._func;
*P._mark = P._scan->mark();
P._mark++;
if( (func)(P, M1._value) == 0 ) {
P._state = Parse::Fail;
}
P._mark--;
}
return P;
}
template <class S, class T> Parse&
operator , (Parse& P, const MATCH2<S, T>& M2)
{
if( P._state == Parse::And) {
int (*func)(Parse&, S&, T&) = M2._func;
*P._mark = P._scan->mark();
P._mark++;
if( (func)(P, M2._value1, M2._value2) == 0 ) {
P._state = Parse::Fail;
}
P._mark--;
}
return P;
}
MATCH0
MATCH( int (*func)(Parse&))
{
return MATCH0(func);
}
template<class T> MATCH1<T>
MATCH(int (*func)(Parse&, T&), T& value1)
{
return MATCH1<T>(func, value1);
}
template<class S, class T> MATCH2<S, T>
MATCH(int (*func)(Parse&, S&, T&), S& value1, T& value2)
{
return MATCH2<S,T>(func, value1, value2);
}